using System;
using System.Xml;
using log4net;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2007-05-01
	Status:		release
	Version:	2.0.0
	Build:		20070501
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070501:
	This is a wrapper class to the log4net logger class. Utilising this class 
	requires the following:
	- log4net configured in the Web.config file
	- the log4net in the bin directory
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX {
	/// <summary>
	/// Description of the x_logger class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class x_logger {
		#region Invisible properties
		private static ILog logger;
		#endregion
		
		#region Constants
		private const string SEPARATOR_PARAM = ",";
		private const string SEPARATOR_PAIR = ":";
		#endregion
		
		#region Visible properties
		#endregion
		
		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_logger() {
			logger = LogManager.GetLogger(typeof(x_logger));
			log4net.Config.XmlConfigurator.Configure();
		}
		/// <summary>Typical constructor</summary>
		/// <param name="type">The System.Type of the class doing the logging</param>
		public x_logger(Type type) {
			logger = LogManager.GetLogger(type);
			log4net.Config.XmlConfigurator.Configure();
		}
		/// <summary>Typical constructor</summary>
		/// <param name="type">The identifier of the thing doing the logging</param>
		public x_logger(string type) {
			logger = LogManager.GetLogger(type);
			log4net.Config.XmlConfigurator.Configure();
		}
		#endregion

		#region Public methods
		/// <overloads>Logs information</overloads>
		/// <summary>Logs single value</summary>
		/// <param name="parameter">Parameter to be logged</param>
		public void Info(string parameter) {
			logger.Info(sanitise(parameter));	// get rid of newlines
		}
		/// <summary>Logs xml document</summary>
		/// <param name="log">Document to be logged</param>
		public void Info(XmlDocument log) {
			Info(log.OuterXml);
		}
		/// <summary>Logs xml element</summary>
		/// <param name="log">Element to be logged</param>
		public void Info(XmlElement log) {
			Info(log.OuterXml);
		}
		/// <summary>Logs xml node</summary>
		/// <param name="log">Document to be logged</param>
		public void Info(XmlNode log) {
			Info(log.OuterXml);
		}
		/// <summary>Logs method and name value pairs</summary>
		/// <param name="method">Method to be logged</param>
		/// <param name="parameters">Array of name value pairs</param>
		public void info(string method, params string[] parameters) {
			for (int i=0; i<parameters.Length; i=i+2) {
				method = String.Concat(method, SEPARATOR_PARAM, parameters[i], SEPARATOR_PAIR, parameters[i+1]);
			}
			Info(method);
		}
		
		/// <overloads>Logs debugging information</overloads>
		/// <summary>Logs single value</summary>
		/// <param name="parameter">Parameter to be logged</param>
		public void Debug(string parameter) {
			logger.Debug(sanitise(parameter));
		}
		/// <summary>Logs method and name value pairs</summary>
		/// <param name="method">Method to be logged</param>
		/// <param name="parameters">Array of name value pairs</param>
		public void Debug(string method, params string[] parameters) {
			for (int i=0; i<parameters.Length; i=i+2) {
				method = String.Concat(method, SEPARATOR_PARAM, parameters[i], SEPARATOR_PAIR, parameters[i+1]);
			}
			Debug(method);
		}
		#endregion
		
		#region Protected methods
		#endregion
		
		#region Private methods
		private string sanitise(string input) {
			input = input.Replace(Environment.NewLine, " ");
			input = input.Replace("\n", "");
			return input;
		}
		#endregion
	}
}
